package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.CommonSearchBean;
import com.fy.fyzf.bean.ExChangeRecordBean;
import com.fy.fyzf.bean.ExchangeBean;
import com.fy.fyzf.bean.ExchangeDetailBean;
import com.fy.fyzf.bean.IntegralRecordBean;
import com.fy.fyzf.bean.IntegralStoreBean;
import com.fy.fyzf.bean.RulesBean;
import com.fy.fyzf.presenter.IntergralPresenter;
import com.fy.fyzf.view.IntergralCallBack;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.adapter.ExchangeRecordAdapter;

public class ExChangeRecordActivity extends BaseActivity<IntergralPresenter> implements IntergralCallBack {


    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    private ExchangeRecordAdapter exchangeRecordAdapter;

    @Override
    protected IntergralPresenter createPresenter() {
        return new IntergralPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_ex_change_record;
    }


    @Override
    public void getIntegralListSuccess(IntegralStoreBean integralStoreBean) {

    }

    private int tag = 1;
    private int page = 1;
    private int pageSize = 10;
    @Override
    public void initView() {
        super.initView();

        setTitleName("兑换记录");
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                tag = 1;
                initData();
            }
        });

        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                tag = 2;
                initData();
            }
        });


        exchangeRecordAdapter = new ExchangeRecordAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(exchangeRecordAdapter);

        exchangeRecordAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<ExChangeRecordBean.ListBean> data = adapter.getData();
                Intent intent = new Intent(ExChangeRecordActivity.this,ExChangeDetailActivity.class);
                intent.putExtra("id", data.get(position).getExchangeId());
                startActivity(intent);
            }
        });
    }
    private CommonSearchBean commonSearchBean = new CommonSearchBean();

    @Override
    public void initData() {
        super.initData();
        commonSearchBean.setPageNum(page);
        commonSearchBean.setPageSize(pageSize);
        mPresenter.getExchangeRecord(commonSearchBean);
    }

    @Override
    public void exchangeSuccess(ExchangeBean exchangeBean) {

    }

    @Override
    public void getExchangeDetailSuccess(ExchangeDetailBean exchangeDetailBean) {

    }

    @Override
    public void getExchangeRecordSuccess(ExChangeRecordBean exChangeRecordBean) {
        smartRefresh.finishRefresh();
        smartRefresh.finishLoadMore();

        if (null == exChangeRecordBean || exChangeRecordBean.getList().size() == 0) {
            exchangeRecordAdapter.setEmptyView(R.layout.layout_nodata_common);
        }
        if (tag == 1) {
            exchangeRecordAdapter.setNewData(exChangeRecordBean.getList());
        } else {
            exchangeRecordAdapter.addData(exChangeRecordBean.getList());
        }
    }

    @Override
    public void getIntegralRecordListSuccess(IntegralRecordBean integralRecordBean) {

    }

    @Override
    public void getRulesSuccess(List<RulesBean> list) {

    }

}